$(function () {
    $("#jqGrid").jqGrid({
        url: 'sys/generator/list',
        datatype: "json",
        colModel: [
            {label: '表名', name: 'tableName', width: 100, key: true},
            {label: '引擎类型', name: 'engine', width: 100},
            {label: '表备注', name: 'tableComment', width: 100},
            {label: '创建时间', name: 'createTime', width: 100}
        ],
        viewrecords: true,
        width: "100%",
        height: '100%',
        rowNum: 15,
        rowList: [15, 30, 50, 100, 200],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

const vm = new Vue({
    el: '#rrapp',
    data: {
        datasource: {
            databaseType: null,
            url: null,
            username: null,
            password: null
        },
        projetModel: {
            author: null,
            email: null,
            package: null,
            moduleName: null,
            tablePrefix: null
        },
        search: {
            tableName: null
        }
    },
    mounted: function () {
        this.initDataSource();
        this.initProjectModel();
    },
    methods: {
        query: function () {
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'tableName': vm.search.tableName},
                page: 1
            }).trigger("reloadGrid");
        },
        generatorCode: function () {
            const tableNames = getSelectedRows();
            if (tableNames == null) {
                return;
            }
            location.href = "sys/generator/code?tables=" + tableNames.join();
        },
        generatorWord: function () {
            const tableNames = getSelectedRows();
            if (tableNames == null) {
                return;
            }
            location.href = "sys/generator/word?tables=" + tableNames.join();
        },
        initDataSource: function () {
            $.get("sys/dataSource/info", function (data, status) {
                if (status === "success" && data.code == 0) {
                    vm.datasource = data.data
                }
            });
        },
        resetDataSource: function () {
            $.post("sys/dataSource/reset", JSON.stringify(vm.datasource), function (data, status) {
                if (status !== "success") {
                    alert("网络异常，请稍后再试")
                    return;
                }
                if (data.code == 0) {
                    vm.datasource = data.data
                } else {
                    alert(data.msg)
                }
            });
        },
        dataSourcePing: function () {
            $.post("sys/dataSource/ping", JSON.stringify(vm.datasource), function (data, status) {
                if (status !== "success") {
                    alert("网络异常，请稍后再试")
                    return;
                }
                if (data.code == 0) {
                    alert(data.msg, function () {
                        vm.query()
                    })
                } else {
                    alert(data.msg)
                }
            });
        },
        initProjectModel: function () {
            $.get("sys/projectModel/info", function (data, status) {
                if (status === "success" && data.code == 0) {
                    vm.projetModel = data.data
                }
            });
        },
        resetProjectModel: function () {
            $.post("sys/projectModel/reset", JSON.stringify(vm.datasource), function (data, status) {
                if (status !== "success") {
                    alert("网络异常，请稍后再试")
                    return;
                }
                if (data.code == 0) {
                    vm.projetModel = data.data
                } else {
                    alert(data.msg)
                }
            });
        },
        saveProjectModel: function () {
            $.post("sys/projectModel/save", JSON.stringify(vm.projetModel), function (data, status) {
                if (status !== "success") {
                    alert("网络异常，请稍后再试")
                    return;
                }
                if (data.code != 0) {
                    alert(data.msg);
                }
            });
        }
    }
});

